<?php

if (!defined('IN_IA')) {
    exit('Access Denied');
}

class Datatab_MxShopPage extends WebPage {

    public function main() {
        global $_W,$_GPC;

        include $this->template('sorder/datatab');
    }

    public function ajaxgetorder(){

        global $_W,$_GPC;
        $uniacid = $_W['platformid'];

        $mcTable = tablename('mc_members');
        $memberTable = tablename('mx_shop_user');
        $memberLogTable = tablename('mx_shop_user_log');
        $cgcOrderTable = tablename('mx_shop_service_order');
        $mendianTable = tablename('mx_shop_mendian');
        $goodsOrderTable = tablename('mx_shop_service_goods_order');
        $comLogTable = tablename('mx_shop_service_commission_log');
        $comResTable = tablename('mx_shop_service_commission_reserved');
        $comResLogTable = tablename('mx_shop_service_commission_reserved_log');

        $order = pdo_fetchall('SELECT t.*, 0.00 as zj, m.realname as realname, m.credit2 as credit, m.mobile as mobile, '.
                    'ifnull((select money from '.$comLogTable.' l where l.ordersn=t.ordersn and l.level=1 limit 1),0) as wd, '.
                    'ifnull((select money from '.$comLogTable.' l where l.ordersn=t.ordersn and l.level=2 limit 1),0) as wyj, '.
                    'ifnull((select money from '.$comLogTable.' l where l.ordersn=t.ordersn and l.level=3 limit 1),0) as qudao, '.
                    'ifnull((select money from '.$comLogTable.' l where l.ordersn=t.ordersn and l.level=4 limit 1),0) as shengji, '.
                    'ifnull((select money from '.$comLogTable.' l where l.ordersn=t.ordersn and l.level=5 limit 1),0) as quyu, '.
                    'ifnull((select money from '.$comLogTable.' l where l.ordersn=t.ordersn and type=2 and l.level=6 limit 1),0) as qiye, '.
                    'ifnull( ifnull((select money from '.$comLogTable.' l where l.ordersn=t.ordersn and l.level=6 limit 1),'.
                            '(select money from '.$memberLogTable.' ml where ml.logno=t.ordersn and ml.id='.
                            '(select ifnull(id, 0) from '.$memberTable.' where id=m.parentid limit 1) limit 1)), 0) as tuijian, '.
                    'ifnull((select mendianname from '.$mendianTable.' d where d.id=t.mendian_id limit 1),0) as mdname, '.
                    'ifnull((select money from '.$memberLogTable.' ml where ml.logno=t.ordersn and ml.id=t.uid limit 1),0) as xiaofei, '.
                    'ifnull((select remain from '.$comResLogTable.' crl where crl.orderid=t.id limit 1),0) as remain, '.
                    'ifnull((select credit2 from '.$mcTable.' c where c.uid=m.uid limit 1),0) as mc_credit '.
                    ' FROM ('.
                    ' select id,uid,ordersn,goodsprice,1 as number,realprice,createtime,mendian_id from '.$cgcOrderTable.' where deleted=0 and status=3'.
                    ' union all '.
                    ' select id,uid,ordersn,goodsprice,number,realprice,createtime,mendian_id from '.$goodsOrderTable.' where deleted=0 and status=3) t'.
                    ' LEFT JOIN '.$memberTable.' m ON t.uid=m.id '.
                    ' WHERE 1 ORDER BY t.createtime DESC LIMIT 100'
                    );


        foreach ($order as $index=>$item) {
            $order[$index]['date'] = date('Y.m.d H.i', $item['createtime']);
            $order[$index]['credit'] = empty(intval($item['credit'])) ? $item['mc_credit'] : $item['credit'];
            $order[$index]['mobile'] = substr_replace($item['mobile'], '****',3, 4);
        }

        $total = pdo_fetchcolumn('SELECT ifnull(count(uid), 0) FROM '.$memberTable);

        $money = pdo_fetchcolumn('SELECT ifnull(sum(t.realprice), 0) FROM ('.
                    ' select id,uid,ordersn,goodsprice,1 as number,realprice,createtime,mendian_id,status from '.$cgcOrderTable.' where deleted=0'.
                    ' union all '.
                    ' select id,uid,ordersn,goodsprice,number,realprice,createtime,mendian_id,status from '.$goodsOrderTable.' where deleted=0 ) t'.
                    ' WHERE t.status>1');

        /*$coll = pdo_fetchcolumn('SELECT ifnull(sum(t.realprice), 0) FROM ('.
            ' select id,uid,ordersn,goodsprice,1 as number,realprice,createtime,mendian_id,status from '.$cgcOrderTable.' where deleted=0'.
            ' union all '.
            ' select id,uid,ordersn,goodsprice,number,realprice,createtime,mendian_id,status from '.$goodsOrderTable.' where AND deleted=0 ) t'.
            ' WHERE t.status=3');*/

        $coll = pdo_fetchcolumn('SELECT ifnull(remain, 0) FROM '.$comResTable.' WHERE 1');

        show_json(1, array('data'=>$order, 'total'=>$total, 'money'=>$money, 'coll'=>$coll));

    }


}
